//about.vue
<template>
    <div class='q-content'>
      <span v-on:click='translate'>hello</span>
      <div class='q-body' v-html='article'>
      </div>
    </div>
</template>


<script>
export default {
  name: 'engP',
  data:function () {
    var a = 'Today is a good day! Today is a good day!';
    var arr = a.split(' ');

    var newa = '';
    for (var i = 0, len = arr.length; i < len; i++) {
      newa += '<span @click.sync="translate(this)">' + arr[i] + '</span>';
    }

    return {
      article: newa
    }
  },
  methods: {
    translate (e) {
      var q = e.target.innerText;
      var from = 'en';
      var to = 'zh-CHS';
      var appKey = '1049825978eb3433';
      var signature = 'hc7FtfuNPC4jSfDUrwi0Dz5T0i5rVIbY';
      var salt = Date.parse(new Date());
      var sign = md5(appKey + q + salt + signature);

      $.ajax({
        url: "http://openapi.youdao.com/api",
        dataType: 'jsonp',
        data: {
          q: q,
          from: from,
          to: to,
          appKey: appKey,
          salt: salt,
          sign: sign
        },
        success: function(result) {
          console.log(result);
        }
      });
    },

    selectRange (e) {

      var userSelection, text;
      if (window.getSelection) {
          //现代浏览器
          userSelection = window.getSelection();
      } else if (document.selection) {
          //IE浏览器 考虑到Opera，应该放在后面
          userSelection = document.selection.createRange();
      }

      if (!(text = userSelection.text)) {
          text = userSelection;
      }

      var q = eval(text);
      q = decodeURI(encodeURI(q.toString()).replace('%0A%0A', " oooo "));

      var from = 'en';
      var to = 'zh';
      var appid = '20170724000067336';
      var signature = '0RatS7aBkL4FnmeayiTZ';
      var salt = Date.parse(new Date());
      var sign = md5(appid + q + salt + signature);

      $.ajax({
        url: "http://api.fanyi.baidu.com/api/trans/vip/translate",
        dataType: 'jsonp',
        data: {
          q: q,
          from: from,
          to: to,
          appid: appid,
          salt: salt,
          sign: sign
        },
        success: function(result) {
          if (result.trans_result.length > 0) {
            var dst = result.trans_result[0].dst;
            dst = dst.replace('噢，','<br/>');
            console.log(dst);
            alert(dst);
          }
        }
      });
    }
  }
}
</script>
